package com.weakteam.orderonline.adminconsole.web;

import com.weakteam.orderonline.dataresource.domain.Goods;
import com.weakteam.orderonline.dataresource.service.GoodsService;
import org.json.simple.JSONArray;
import org.json.simple.JSONObject;
import org.json.simple.parser.JSONParser;
import org.json.simple.parser.ParseException;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.RequestMapping;

import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import java.io.IOException;
import java.io.PrintWriter;
import java.util.Iterator;
import java.util.List;

/**
 * @author Nail
 */

@Controller
public class GoodsNetService {

    @Autowired
    GoodsService goodsService;

    private static final Logger log = LoggerFactory.getLogger(GoodsNetService.class);

    private static void initJSONHeader(final HttpServletResponse httpResponse){
        httpResponse.setHeader("Connection", "close");
        httpResponse.setHeader("Content-Type", "application/json");
        httpResponse.setHeader("Expires", "Thu, 19 Nov 1981 08:52:00 GMT");
        httpResponse.setHeader("Cache-Control", "no-store, no-cache, must-revalidate, post-check=0, pre-check=0");
        httpResponse.setHeader("Pragma", "no-cache");
        httpResponse.setCharacterEncoding("UTF-8");
    }

    // Goods
    @RequestMapping(value = "/getGoodsData")
    public void getGoodsData(final HttpServletRequest httpRequest, final HttpServletResponse httpResponse) {
        try {
            Integer menuId = Integer.parseInt(httpRequest.getParameter("menuId"));

            initJSONHeader(httpResponse);
            PrintWriter out = httpResponse.getWriter();
            List<Goods> goods = goodsService.getAllByMenuId(menuId);
            out.print("[");
            for (Iterator<Goods> it = goods.iterator(); it.hasNext();) {
                Goods good = it.next();
                out.print(good.getJSON());
                if (it.hasNext()) out.println(",");
            }
            out.print("]");
            out.flush();
            out.close();
        } catch (IOException ex) {
            log.error("getGoodsData {}", ex);
        }
    }

    @RequestMapping(value = "/saveGoodsDataOne")
    public void saveGoodsDataOne(final HttpServletRequest httpRequest, final HttpServletResponse httpResponse) {
        try {
            String goodsJson = httpRequest.getParameter("data");
            Goods goods = new Goods();
            goods.setJSON(goodsJson);

            this.goodsService.save(goods);

            initJSONHeader(httpResponse);
            PrintWriter out = httpResponse.getWriter();
            out.print("");
            out.flush();
            out.close();
        } catch (IOException ex) {
            log.error("saveGoodsDataOne {}", ex);
        }
    }

    @RequestMapping(value = "/saveGoodsData")
    public void saveGoodsData(final HttpServletRequest httpRequest, final HttpServletResponse httpResponse) {
        try {
            String json = httpRequest.getParameter("data");

            JSONParser parser = new JSONParser();
            JSONArray jData = (JSONArray)parser.parse(json);
            for(Iterator<JSONObject> it = jData.iterator(); it.hasNext();){
                JSONObject jObj = it.next();
                Goods goods = new Goods();
                goods.setJSON(jObj);
                this.goodsService.save(goods);
            }

            initJSONHeader(httpResponse);
            PrintWriter out = httpResponse.getWriter();
            out.print("");
            out.flush();
            out.close();
        } catch (ParseException ex) {
            log.error("saveGoodsData parse exception {}", ex);
        } catch (IOException ex) {
            log.error("saveGoodsData io exception {}", ex);
        }
    }

    @RequestMapping(value = "/deleteGoodsData")
    public void deleteGoodsData(final HttpServletRequest httpRequest, final HttpServletResponse httpResponse) {
        try {
            Integer id = Integer.parseInt(httpRequest.getParameter("id"));

            this.goodsService.delete(id);

            initJSONHeader(httpResponse);
            PrintWriter out = httpResponse.getWriter();
            out.print("");
            out.flush();
            out.close();

        } catch (IOException ex) {
            log.error("deleteGoodsData {}", ex);
        }
    }

}
